<?php
require_once 'pages/TMSFrontUserPage.inc';
require_once 'util/TFormPersistance.inc';

class TMSFrontProfilePage extends TMSFrontUserPage {
/**
 * @var TMSClientModel
 */	
	protected $_ioc_client_model_;
	
	protected $_array_hide_properties_ = array();
	
	private $_formdata_;
	
	public function __construct($path, $filename, TIntrinsicPageService $service){
		parent::__construct($path, $filename, $service);
		$this->_formdata_ = new TFormPersistance($this); 
	}
	
	public function Properties(){
		$item = $this->ClientModel->GetItem($this->service->Acl->CurrentUser()->Id);
		if ($this->exception)
			foreach ($this->_formdata_->FormData() as $nm=>$value)
				$item->$nm = $value;
/*
 * @todo refactor this copypaste 
 */		
		return array_udiff($item->Properties(),$this->_array_hide_properties_,create_function('$a,$b','return strcmp(($a instanceof IPropertyMeta)?$a->Name():(string)$a,($b instanceof IPropertyMeta)?$b->Name():(string)$b);'));
	}
	
	public function http_ChangePassword(){
		if (!$this->request->pwd)
			throw new TExposableException('Пароль не может быть пустым!');
			
		if ($this->request->pwd != $this->request->pwd2)	
			throw new TExposableException('Подтверждение пароля не совпадает с паролем!');
			
		$this->service->Acl->SetPassword($this->service->Acl->CurrentUser()->Id,$this->request->pwd);	
	}
	
	public function http_Save(){
		$props = $this->ClientModel->ClassProperties();
		$this->_formdata_->SaveProperties($props,$data);
		
		foreach ($props as $p){
			$nm = $p->Name();
			if (is_null($this->request->$nm) && $p->Required())
				throw new TExposableException('Поле "'._i($nm).'" не может быть пустым!');
		}
		$this->ClientModel->EditItem($this->service->Acl->CurrentUser()->Id,$data);
		if ($this->service->Acl instanceof TMSACL)
			$this->service->Acl->ReloadProfile();
		$this->_formdata_->Clear();
	}
}

